## Final plotgapminder_brazil |>ggplot(aes(x = year, y = lifeExp)) +geom_point() +labs(title ='Life expectancy evolution in Brazil',subtitle ='From 1952 to 2007',x ='Years',y ='Life expectancy(age)',caption ='Source: gapminder dataset') +theme_light() +scale_x_continuous(breaks =seq(1952, 2007, by =5)) +geom_line()
Faceting: facet_wrap
What if we need more than one country? (e.g.: Chile)
Do we need to copy the whole, paste it and change the country’s name only?
## Plotting the first layer of our plotgapminder2 <- gapminder |>filter(country %in%c('Brazil', 'Chile'))## Plotting it!gapminder2 |>ggplot(aes(x = year, y = lifeExp)) +geom_point() +labs(title ='Life expectancy evolution in Brazil & Chile',subtitle ='From 1952 to 2007',x ='Years',y ='Life expectancy(age)',caption ='Source: gapminder dataset') +theme_light() +scale_x_continuous(breaks =seq(1952, 2007, by =5)) +geom_line() +## We know in advance that our data contains the column named 'country'facet_wrap(~country)
gglpot2: themes
When using ggplot2, we can:
Use predefined themes
Make our own basic theme adjustments
Add some elements on top of a theme that we liked
Before we dive in, let me show you this!
## We can create a ggplot object and keep building on top of it later on!our_plot <- gapminder_brazil |>ggplot(aes(x = year, y = lifeExp)) +geom_point() +labs(title ='Life expectancy evolution in Brazil',subtitle ='From 1952 to 2007',x ='Years',y ='Life expectancy(age)',caption ='Source: gapminder dataset') +theme_light() +scale_x_continuous(breaks =seq(1952, 2007, by =5)) +geom_line()
Using predefined themes
ggplot2 built-in themes
The ggthemes package
## Building on top of the ggplot variable we've created# The `ggthemes` package contains a fair amount of different themes# Nice way of quickly customizing our plot with one line of codeour_plot +# Using a theme from the `ggthemes` packagetheme_economist()
Making small adjustments
Some elements: element_text, element_line…
Customizing legend and background
## Remember, we are building on top of the ggplot variable we've createdour_plot +## Now, let's make some little improvementstheme_light() +# Setting the position of the legend to be on the `top`theme(legend.position ="top",## Adding a rectangular element in the background of the legendlegend.background =element_rect(fill ="lightblue",colour ="darkblue",size =1,linetype ="solid"),## Adding a rectangular element in the plot and panel backgroundplot.background =element_rect(fill ="lavender"),panel.background =element_rect(fill ="ivory"),## Setting the size of the font in the text with element_texttext =element_text(size =12),## Bolding the text of the title of the axis with element_textaxis.title =element_text(face ="bold"))
Interesting features!
Highlighting some aspects of our graph!
## Remember, we are building on top of the ggplot variable we've createdour_plot +## Setting the color of the points to equal a condition (odd years)geom_point(aes(color = year %%2==1)) +## Coloring the result of the condition of black or redscale_color_manual(values =c("FALSE"="black", "TRUE"="red"), guide =FALSE) +## Adding a text label for the even yearsgeom_text(aes(label =ifelse(year %%2==0,as.character(ceiling(lifeExp)),"")), hjust =0.5, vjust =2, color ="black", size =4)